package com.example.hosptialtag.service;

import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author liuyuhao&caiang
 * @Date 2022/2/21
 */

@Service
public class CheckCardService {//验证身份证和医保卡

    public boolean idcardValidate(String idcard) {
        String[] ValCodeArr = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};//验证码表 前17位累加%11作为下标
        String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};
        String Ai = "";
        if (idcard.length() != 18) {
            return false;
        }
        if (idcard.length() == 18) {
            Ai = idcard.substring(0, 17);
        }
        if (isNumeric(Ai) == false) {//日期无效
            return false;
        }

        //出生年月是否有效
        String strYear = Ai.substring(6, 10);// 年份
        String strMonth = Ai.substring(10, 12);// 月份
        String strDay = Ai.substring(12, 14);// 日


        if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) {
            return false;
        }
        GregorianCalendar gc = new GregorianCalendar();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150 || (gc.getTime().getTime() - s.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
                //生日不在有效范围
                return false;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {//月份无效
            return false;
        }
        if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) { //日期无效
            return false;
        }
        //最后一位验证码是否有效
        int TotalmulAiWi = 0;
        for (int i = 0; i < 17; i++) {
            TotalmulAiWi = TotalmulAiWi + Integer.parseInt(String.valueOf(Ai.charAt(i))) * Integer.parseInt(Wi[i]);
        }
        int modValue = TotalmulAiWi % 11;//累加
        String strVerifyCode = ValCodeArr[modValue];
        Ai = Ai + strVerifyCode;

        if (idcard.length() == 18) {
            if (Ai.equals(idcard) == false) {
                return false;
            }
        } else {
            return true;
        }
        return true;
    }
    private static boolean isNumeric(String str) {//判断String是否是数字
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }
    public static boolean isDate(String strDate){//判断是否是日期
        Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
        Matcher m = pattern.matcher(strDate);
        return m.matches();
    }

    public boolean Sex(String idcard){//看17位，奇数为男
        if(Integer.parseInt(idcard.substring(16,17))%2==0){
            System.out.println("女");
            return true;
        }
        System.out.println("男");
        return false;
    }
    public boolean isNoAdult(String idcard){//是否未成年  看出生年月，未成年要生成有时间限制的标签
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(idcard.substring(6,10)) + 18,    //出生年份加上18，和现在比
                Integer.parseInt(idcard.substring(10,12)) - 1,
                Integer.parseInt(idcard.substring(12,14)));
        long l = System.currentTimeMillis() - cal.getTimeInMillis();
        System.out.println(l);
        return 0 > l;
    }

    public String adultTime(String idcard){//成年时间
        String at = Integer.toString(Integer.parseInt(idcard.substring(6,10))+18);
        at += "-"+idcard.substring(10,12)+"-"+idcard.substring(12,14)+" 00:00:00";
        return at;
    }
    public String birthday(String idcard){//生日时间
        String at = Integer.toString(Integer.parseInt(idcard.substring(6,10))+18);
        at += "-"+idcard.substring(10,12)+"-"+idcard.substring(12,14);
        return at;
    }

    public boolean medicardValidate(String medicard){//医保卡合格
        if(medicard.length()==16&&isNumeric(medicard)) {
            return true;
        }
        return false;
    }
    public boolean isLocal(String medicare){//医保卡 ，假设最后1位，1为本地，0为外地
        if(Integer.parseInt(medicare.substring(15,16))==1) {
            return true;
        }
            return false;
    }

}